/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.ui.form;

import com.rcnss.data.Categorie;
import com.rcnss.data.Intervention;
import com.rcnss.data.Reclamation;
import com.rcnss.ui.application.main.MainApp;
import com.rcnss.ui.form.field.factory.InterventionFieldFactory;
import com.rcnss.ui.form.field.factory.ReclamationFieldFactory;
import com.rcnss.ui.table.handler.GeneralActionHandler;
import com.rcnss.ui.table.handler.ReclamtionAssignedActionHandler;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Guesmi
 */
public class AsignReclamationListForm extends AbstractForm {
    Table table = new Table();
    private IndexedContainer userContainer = new IndexedContainer();

    private static String[] formVisibleCols = {
        "Titre", "Cree Par", "Date", "Affecte pour", "Categorie", "Etat"};

    public AsignReclamationListForm()
    {
        initTop();
        initLayout();
        initTable();
       // this.splitPanel.setSplitPosition(80);
    }

     private void initTop()
    {
        for(final String pn : formVisibleCols)
        {
            final TextField sf = new TextField();
            this.top.addComponent(sf);
            sf.setWidth("100%");
            sf.setInputPrompt(pn);
            sf.setImmediate(true);
            this.top.setExpandRatio(sf, 1);
            sf.addListener(new Property.ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
                    userContainer.removeContainerFilters(pn);
                    if (sf.toString().length() > 0 && !pn.equals(sf.toString())) {
                        userContainer.addContainerFilter(pn, sf.toString(),
                                true, false);
                    }
                    getWindow().showNotification(
                            "" + userContainer.size() + " champs trouves",
                            Notification.TYPE_TRAY_NOTIFICATION);
                }
            });
        }
    }

    private void initTable()
    {
        this.table.setWidth("100%");
        this.table.setPageLength(18);
        // selectable
        table.setSelectable(true);
        table.setMultiSelect(false);
        table.setImmediate(true); // react at once when something is selected
        // connect data source
        initContainer();
        table.setContainerDataSource(this.userContainer);
        // turn on column reordering and collapsing
        this.table.setColumnReorderingAllowed(true);
        this.table.setColumnCollapsingAllowed(true);
        // set column headers
        this.table.addActionHandler(new ReclamtionAssignedActionHandler());

        this.table.addListener(new ItemClickListener() {
            public void itemClick(ItemClickEvent event) {
                if(event.getButton()==ItemClickEvent.BUTTON_LEFT)
                {
                    Object id = event.getItemId();
                    final Intervention r = Intervention.SearchByRecID((Integer)id).get(0);
                    r.setREC_ID((Integer)id);
                    if(r.getINT_DATE() == null)
                        r.setINT_DATE((new Date()).toString());
                    EditForm f = new EditForm(r,
                            new InterventionFieldFactory(true));

                    final Window w = new Window();
                    w.setModal(true);
                    //w.setCaption("Reclamation <"+r.getREC_TITRE()+">");
                    w.getContent().addComponent(f);
                    w.getContent().setSizeUndefined();
                    getWindow().addWindow(w);
                    f.cancelChanges.setCaption("Fermer");
                    f.saveChanges.addListener(new Button.ClickListener() {

                        public void buttonClick(ClickEvent event) {
                            try {
                                r.SaveToDB();
                            } catch (Exception ex) {
                                getWindow().showNotification("Erreur de sauvegarde", Notification.TYPE_ERROR_MESSAGE);
                            }
                        }
                    });
                    f.cancelChanges.addListener(new Button.ClickListener() {

                        public void buttonClick(ClickEvent event) {
                            getWindow().removeWindow(w);
                        }
                    });
                }
            }
        });

        left.addComponent(this.top);
        left.addComponent(this.table);
        this.top.setWidth("100%");
        this.body.replaceComponent(this.splitPanel, left);
    }

     private void initContainer()
    {
        this.userContainer.addContainerProperty("Titre", String.class, null);
        this.userContainer.addContainerProperty("Cree par", String.class, null);
        this.userContainer.addContainerProperty("Date", String.class, null);
        this.userContainer.addContainerProperty("Affectee pour", String.class, null);
        this.userContainer.addContainerProperty("Categorie", String.class, null);
        this.userContainer.addContainerProperty("Etat", String.class, null);
        int i =0;
        for(Reclamation u : Reclamation.SearchByAssignedTo(MainApp.UserSessionData.ConnectedUserID))
        {
            Item item = this.userContainer.addItem(u.getREC_ID());
            item.getItemProperty("Titre").setValue(u.getREC_TITRE());
            item.getItemProperty("Cree par").setValue(u.getUTL_ID());
            item.getItemProperty("Date").setValue(new Date());
            item.getItemProperty("Affectee pour").setValue(u.getREC_AFF_A());
            item.getItemProperty("Categorie").setValue(Categorie.SearchByID(u.getCAT_ID()).GetLabelCategorie());
            item.getItemProperty("Etat").setValue(u.getEtatReclamation());
        }
    }

}
